<?php
include 'lib/controllerWithoutGetOrPost.php';
include 'lib/LoggedIn.php';
$loggedIn = new LoggedIn();
$loggedIn->setPermission = "Admin Area";
if(!$loggedIn->check()) {
    echo $loggedIn->message();
} else {
require_once 'header2.php';
function getRemembranceComment($commentId)
{
    global $link;
    $queryrem = "Select RemembranceComment.PersonID, RemembranceComment.Emailed, RemembranceComment.FirstName, RemembranceComment.Surname, RemembranceComment.Email, Person.FirstNames as PersonFirst, Person.Surname as PersonSurname from RemembranceComment left join Person on Person.ID = RemembranceComment.PersonID where RemembranceComment.ID = '".mysql_real_escape_string($commentId)."'";
    $resultrem = mysql_query($queryrem, $link);
    $row = mysql_fetch_assoc($resultrem);
    return $row;
}
function setCommentEmailed($commentId)
{
    global $link;
    $updatenow = "Update RemembranceComment Set Emailed = 1 where RemembranceComment.ID = '".mysql_real_escape_string($commentId)."'"; 
    $success = mysql_query($updatenow, $link);
    return $success;
}
function setCommentCategory($commentId, $category)
{
    global $link;
    $updatequery = "Update RemembranceComment Set Category = ".$category." where RemembranceComment.ID = '".mysql_real_escape_string($commentId)."'";
    return mysql_query($updatequery, $link);
}
function getCategoryCount($category)
{
    global $link;
    $querycount = "select count(*) from RemembranceComment where Category = ".mysql_real_escape_string(catOrOne($_GET['cat'])); 
    $resultcount = mysql_query($querycount, $link);
    return mysql_result($resultcount, 0);
}
function getCommentsByCategory($category, $limit)
{
    global $link;
    $comments = array();
    $query = "select 	RemembranceComment.ID,
RemembranceComment.PersonID, 
	RemembranceComment.Surname, 
	RemembranceComment.FirstName, 
	RemembranceComment.Heading, 
	RemembranceComment.Comment, 
	RemembranceComment.CityTown, 
	RemembranceComment.DateAdded,
	RemembranceComment.DateModified,
	RemembranceComment.Email,
	RemembranceComment.Category,
  Country.Name,
	RemembranceComment.Heading from RemembranceComment left join Country on Country.ID = RemembranceComment.CountryID where Category = ".$category."
  order by DateAdded DESC";
    $result = mysql_query($query, $link);
    while ($row = mysql_fetch_assoc($result)) {
        $comments[] = $row;
    }
    return $comments;
}
function pageNavLink($totalpages, $pagenumber, $currentpage, $cat) {
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"remembrancecomment.php?cat=".$cat."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}

function insertSearchPageTopNavigation($cat, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $cat);
echo pageNavLink($totalpages, $page2, $currentpage, $cat);
echo pageNavLink($totalpages, $page3, $currentpage, $cat);
echo pageNavLink($totalpages, $page4, $currentpage, $cat);
echo pageNavLink($totalpages, $page5, $currentpage, $cat);
echo "...\n\t";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
						
	
}

function insertSearchPageBottomNavigation($cat, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $cat);
echo pageNavLink($totalpages, $page2, $currentpage, $cat);
echo pageNavLink($totalpages, $page3, $currentpage, $cat);
echo pageNavLink($totalpages, $page4, $currentpage, $cat);
echo pageNavLink($totalpages, $page5, $currentpage, $cat);
echo "...\n\t";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"remembrancecomment.php?cat=".$cat."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";					
}

function catOrOne($val) {
  if(!empty($val)) {
    return mysql_real_escape_string($val);
  } else {
    return 1;
  }
}


//20 per page

for($i = 1; $i <= 20; $i++) {
    
  $c1 = $_POST['c'.$i];
  
  if(!empty($c1)) {
    $items = explode("_",$c1);
    $tempid = $items[0];
    $change = $items[1];
    
    if($change == 2) {
    //2 == accepted
    $currow = getRemembranceComment($tempid);
    
  
$Name = "A Memory Tree Team"; //senders name
$email = "theteam@amemorytree.co.nz"; //senders e-mail adress
$recipient = $currow['Email']; //recipient
$subject = "A Memory Tree - your message is online!"; //subject  
    

    //begin of HTML message
    $personID = $db->get($currow['PersonID']);
    $firstname = $db->get($currow['PersonFirst']);
    $surname = $db->get($currow['PersonSurname']);
    $fullname = implode(', ', array($firstname, $surname));
    $message = <<< EOT
<html>
<body>
    <div style="width:550px; margin:0 auto;">
        <h2 style="color: red">Your Message is Online!</h2>
        <p>Thank you for leaving a comment on ($fullname's) Remembrance Page. Your message has been added and is available for <a href="http://www.amemorytree.co.nz/remembrance.php?personid=$personID">viewing</a>.</p>
        <p>A Remembrance Page is a great way to remember someone who has passed away and is an important resource for friends and family both today and in the future.</p>
        <p><a href="mailto:?subject=A Memory Tree has a Remembrance Page open for $fullname&body=Hi, I'm letting you know that a Remembrance Page has been opened on www.amemorytree.co.nz website and you can leave a message for free today.
Visit http://amemorytree.co.nz/remembrance.php?personid=$personID to view the page and read other messages.">Tell your family</a> and friends so that as many people as possible can share their memories at <a href="http://amemorytree.co.nz">amemorytree.co.nz</a>.</p>
    <p>Many thanks for your support.</p>
    <p><a href="mailto:theteam@amemorytree.co.nz">theteam@amemorytree.co.nz</a></p>
    </div>  
    <div style="background-color: #E6F1F9; padding: 15px">
        <p><strong>Did this email go into your junk mail folder on Microsoft Outlook?</strong><br>
            You can either</p>
        <p>Add theteam@amemorytree.co.nz to your address book to make sure our emails arrive in your inbox or,</p>
        <p>Simply drag the mail from your junk mail box to your inbox. This now accepts all future mail from theteam@amemorytree.co.nz</p>
        <p><strong>Please don't reply to this email</strong><br>
            If you would like to contact us please go through the <a href="http://amemorytree.co.nz">website</a>.</p>
    </div>
    <div style="padding: 15px">
        <p>A Memory Tree Limited &copy; 2006-2009<br>
            <a href="http://amemorytree.co.nz/terms.php">Terms</a>
            <a href="http://amemorytree.co.nz/privacy.php">Privacy</a>
            <a href="http://amemorytree.co.nz/codeofethics.php">Code of Ethics</a>
            <a href="http://amemorytree.co.nz/safety.php">Safety</a>
            <a href="http://amemorytree.co.nz/aboutus.php">About</a>
            <a href="http://amemorytree.co.nz/contactus.php">Contact Us</a></p> 
        </div>
    </div>
</body>
</html>

EOT;
    //end of message



    // To send the HTML mail we need to set the Content-type header.
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers  .= "From: ". $Name . " <" . $email . ">\r\n";
        
    // now lets send the email.
      if($currow['Emailed'] == 0) { 
        mail($recipient, $subject, $message, $headers); //mail command :)
        $updatenowresult = setCommentEmailed($tempid);
      }
   
    }
    
    
    $updateresult = setCommentCategory($tempid, $change);
  }
  
}

$resultcount = getCategoryCount($_GET['cat']);

$totalpages = 1;
$resultsperpage = 20;
$totalpages = ceil($resultcount/$resultsperpage);
if($totalpages == 0) {
  $totalpages = 1;
}


$from = 0;
$to = $resultsperpage;
$currentpage = 1;

if(isset($_GET['page'])) {
	$currentpage = $_GET['page'];
	$to = $_GET['page']*$resultsperpage;
	$from = $to-$resultsperpage;
	$limit = ' LIMIT '.$from.",".$resultsperpage;	

} else {
	$limit = ' LIMIT '."0,".$resultsperpage;
}

$pagenumber = $_GET['page'];


if($_GET['page'] > 0) {
	if($_GET['page'] < $totalpages) {
		$pagenumber = $_GET['page']+1;
	}	
} else {
	$pagenumber = $_GET['page']+2;
}


$class1 = "odd";
$class2 = "even";
$class = $class1;

echo "<br /><br /><div class=\"cats\"><ul><li><a href=\"remembrancecomment.php?cat=1\">New</a></li>
<li><a href=\"remembrancecomment.php?cat=2\">Accepted</a></li>
<li><a href=\"remembrancecomment.php?cat=3\">Denied</a></li>
<li><a href=\"remembrancecomment.php?cat=4\">Unknown</a></li></ul></div>";

  insertSearchPageTopNavigation($_GET['cat'], $totalpages, $currentpage);

echo "<form action=\"remembrancecomment.php\" method=\"post\"><input type=\"submit\" value=\"Submit\"><br /><table><tr><th>ID</th><th>Category</th><th>Surname</th><th>FirstName</th><th>Heading</th><th>Comment</th><th>CityTown</th><th>Email</th><th>Date Added</th><th>Date Modified</th><th>Remembrance Page</th></tr>\n";

function outputCategories($value, $num, $id) {
  if($value == 1) {
  //unchosen
  return "<ul><li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_2\" id=\"".$id."_2\" /><label for=\"".$id."_2\" >Accept</label></li>". 
  "<li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_3\" id=\"".$id."_3\" /><label for=\"".$id."_3\" >Deny</label></li>". 
  "<li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_4\" id=\"".$id."_4\" /><label for=\"".$id."_4\" >Unknown</label></li></ul>"; 
  
  } else if ($value == 2) {
  //accepted
  return "<ul><li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_3\" id=\"".$id."_3\" /><label for=\"".$id."_3\" >Deny</label></li>". 
  "<li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_4\" id=\"".$id."_4\" /><label for=\"".$id."_4\" >Unknown</label></li></ul>"; 
 
 } else if ($value == 3) {
  return "<ul><li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_2\" id=\"".$id."_2\" /><label for=\"".$id."_2\" >Accept</label></li>". 
  "<li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_4\" id=\"".$id."_4\" /><label for=\"".$id."_4\" >Unknown</label></li></ul>"; 
  
  } else if ($value == 4) {
  //unknown - review later
  return "<ul><li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_2\" id=\"".$id."_2\" /><label for=\"".$id."_2\" >Accept</label></li>". 
  "<li><input type=\"radio\" name=\"c".$num."\" value=\"".$id."_3\" id=\"".$id."_3\" /><label for=\"".$id."_3\" >Deny</label></li></ul>";
  }  
}

$comments = getCommentsByCategory(catOrOne($_GET['cat']), $limit);

$rowcount = 0;
foreach ($comments as $row) {
$rowcount++;


echo "<tr class=\"$class\">";
echo "<td>".$row['ID']."</td>\n";
echo "<td>".outputCategories($row['Category'], $rowcount, $row['ID'])."</td>";
echo "<td>".str_replace("&", "&amp;", $row['Surname'])."</td>";
echo "<td>".str_replace("&", "&amp;", $row['FirstName'])."</td>";
echo "<td>".stripslashes(str_replace("&", "&amp;", $row['Heading']))."</td>";
echo "<td width=\"300px\">".nl2br(StripSlashes(str_replace("&", "&amp;", $row['Comment']))).' <a href="editComment.php?commentid='.$row['ID'].'">Edit</a></td>';
echo "<td>".stripslashes(str_replace("&", "&amp;", $row['CityTown']))."</td>";
echo "<td>".str_replace("&", "&amp;", $row['Email'])."</td>";
echo "<td>".str_replace("&", "&amp;", $row['DateAdded'])."</td>";
echo "<td>".str_replace("&", "&amp;", $row['DateModified'])."</td>";
echo "<td><a href=\"../remembrance.php?personid=".str_replace("&", "&amp;", $row['PersonID'])."\">Remembrance Page</a></td>";
echo "</tr>\n";

if($class == $class1) {
	$class = $class2;
} else if ($class == $class2) {
	$class = $class1;
}

}
echo "</table><input type=\"submit\" value=\"Submit\">
</form>";

  insertSearchPageBottomNavigation($_GET['cat'], $totalpages, $currentpage);
echo "</body></html>";
}
?>
